package cn.edu.hubu.hubusmartmedicalsystem.service.impl;

import cn.edu.hubu.hubusmartmedicalsystem.entity.Device;
import cn.edu.hubu.hubusmartmedicalsystem.mapper.DeviceMapper;
import cn.edu.hubu.hubusmartmedicalsystem.service.DeviceService;
import cn.edu.hubu.hubusmartmedicalsystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public ResultVO<Device> getDeviceById(String deviceId) {
        Device device = deviceMapper.selectById(deviceId);
        if (device == null) {
            return new ResultVO<>(500, "设备不存在", null);
        }
        return new ResultVO<>(200, "查询成功", device);
    }

    @Override
    public ResultVO<List<Device>> getAllDevices() {
        List<Device> devices = deviceMapper.selectAll();
        if (devices == null || devices.isEmpty()) {
            return new ResultVO<>(500, "没有找到任何设备", null);
        }
        return new ResultVO<>(200, "查询成功", devices);
    }

    @Override
    public ResultVO<String> addDevice(Device device) {
        if (deviceMapper.selectById(device.getDeviceId()) != null){
            return new ResultVO<>(500,"添加失败，该设备已存在",null);
        }
        int result = deviceMapper.insertDevice(device);
        if (result > 0) {
            return new ResultVO<>(200, "设备添加成功", null);
        }
        return new ResultVO<>(500, "设备添加失败", null);
    }

    @Override
    public ResultVO<String> updateDevice(Device device) {
        int result = deviceMapper.updateDevice(device);
        if (result > 0) {
            return new ResultVO<>(200, "设备更新成功", null);
        }
        return new ResultVO<>(500, "设备更新失败", null);
    }

    @Override
    public ResultVO<String> deleteDevice(String deviceId) {
        int result = deviceMapper.deleteById(deviceId);
        if (result > 0) {
            return new ResultVO<>(200, "设备删除成功", null);
        }
        return new ResultVO<>(500, "设备删除失败", null);
    }

    @Override
    public ResultVO<String> addDevices(List<Device> devices) {
        int result = deviceMapper.insertDevices(devices);
        if (result > 0) {
            return new ResultVO<>(200, "批量设备添加成功", null);
        }
        return new ResultVO<>(500, "批量设备添加失败", null);
    }

    @Override
    public ResultVO<String> deleteDevices(List<String> deviceIds) {
        int result = deviceMapper.deleteDevices(deviceIds);
        if (result > 0) {
            return new ResultVO<>(200, "批量设备删除成功", null);
        }
        return new ResultVO<>(500, "批量设备删除失败", null);
    }
}
